#include "db.h"
#include "utilities.h"
#include "pass1.h"
#include "pass2.h"

/* the author of the project are:
 * name: dmitry polyak, id: 308967835
 * name: itai kreisler, id: 312427875 */


/* try to open name.as */
static int tryToOpen(char *name);

/* after lable name the is : wichout space between them
 * at operand like ...[...] there is no space in him 
 * the argument need to be which out ending (out but not out.as) */
int main(int argc, char *argv[])
{
	int i = 1;
	
	
	while(i<argc)
	{
		if(tryToOpen(argv[i]) == false)
		{
			fprintf(stderr,"no such file: %s.as\n",argv[i]);
			i++;
			continue;
		}
		clear_list();
		undo_error();
		fprintf(stderr,"at file: %s.as\n",argv[i]);
		pass1(argv[i]);
		pass2(argv[i]);
		removeFile(argv[i],".ob1");
		if(check_error() == false)
		{
			fprintf(stderr,"no errors\n");
		}
		else
		{
			removeFile(argv[i],".ext");
			removeFile(argv[i],".ent");
			removeFile(argv[i],".ob");
		}
		i++;
	}
	
	return 0;
}

/* try to open name.as */
static int tryToOpen(char *name)
{
	FILE *tmp;
	tmp = openFile(name,".as","r");
	if(tmp == NULL)
	{
		return false;
	}
	fclose(tmp);
	return true;
}